<?php

namespace App\Exceptions;

use App\Lib\Code;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use Exception;
use Illuminate\Support\Facades\Log;
use Throwable;
use App\Facades\RechargeLog;

class RechargeGatewayException extends Exception
{
    protected $today;
    //
    public function __construct($class, $resBody, $errMsg = '', $code = 0, \Throwable $previous = null)
    {
        if ($class instanceof RechargeBaseAbstract) {
            $log_str = $class->getLogStr();
            $this->message = '['.$class->getIdentify().']:'.$errMsg. "\r\n {$log_str} \r\n [res]:". $resBody ."\r\n";
        }
        if (is_string($class)) {
            $this->message = $class;
        }
        $this->today = date('Ymd');
    }


    public function useFile()
    {
        switch ($this->getCode()) {
            case Code::RECHARGE_THIRD_LOG:
                Log::useFiles(storage_path().'/logs/recharge/third-party/'.$this->today.'.log');
                break;
            case Code::RECHARGE_MCH_LOG:
                Log::useFiles(storage_path().'/logs/recharge/merchant/'.$this->today.'.log');
                break;
            default:
                Log::useFiles(storage_path().'/logs/recharge/common-'.$this->today.'.log');
                break;
        }
    }
}
